﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;

//using Idstar = Idstar_clw;

namespace SSO
{
    /// <summary>
    /// Summary description for Auth
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Auth : System.Web.Services.WebService
    {

        //[WebMethod]
        //public string HelloWorld()
        //{
        //    return "Hello World";
        //}

        public Idstar.IIdentityManager IdManager
        {
            get 
            {
                Idstar.IIdentityManager idManager 
                    = Server.CreateObject("Idstar.IdentityManager") as Idstar.IIdentityManager;
                return idManager;
            }
        }
        [WebMethod]
        public string GetLogonUrl(string gotoUrl)
        {
            try
            {
                return string.Format("{0}?goto={1}", IdManager.GetLoginURL(), Server.UrlEncode(gotoUrl));
            }
            catch
            {
                return string.Empty;
            }
        }
        [WebMethod]
        public string GetLogoutUrl(string gotoUrl)
        {
            try
            {
                return string.Format("{0}?goto={1}", IdManager.GetLogoutURL(), Server.UrlEncode(gotoUrl));
            }
            catch
            {
                return string.Empty;
            }
        }
        [WebMethod]
        public string GetCurrentUser(string cookieValue)
        {
            try
            {
                return IdManager.GetCurrentUser(Server.UrlDecode(cookieValue));
            }
            catch
            {
                return string.Empty;
            }
        }

        [WebMethod]
        public string GetGroupByUserId(string userID)
        {
            try
            {
                object[] group= IdManager.GetUserAttribute(userID, "dn") as object[];
                if(group!=null && group.Length>0)
                    return group[0].ToString();
                return string.Empty;
            }
            catch
            {
                return string.Empty;
            }
        }

        [WebMethod]
        public object GetGroupByCookieValue(string cookieValue)
        {
            try
            {
                string userID = GetCurrentUser(cookieValue);
                return GetGroupByUserId(userID);
            }
            catch
            {
                return null;
            }
        }
       
        public bool UserIsOnline()
        {

            int shtI;

            string valueC = string.Empty;


            HttpRequest Request = HttpContext.Current.Request;

            for (shtI = 0; shtI < Request.Cookies.Count; shtI++)
            {

                if (Request.Cookies[shtI].Name.CompareTo("iPlanetDirectoryPro") == 0)
                {

                    valueC = Request.Cookies[shtI].Value;

                }

            }

            if (string.IsNullOrEmpty(valueC))
                return false;

            //验证cookie值

            string currentuser = IdManager.GetCurrentUser("" + Server.UrlDecode(valueC));

            if (currentuser != null && currentuser != string.Empty)
            {
                return true;
            }
            else
            {
                return false;
            }      
            return false;
        }
    }
}
